package org.csu.myProject.web.servlet;

import org.csu.myProject.domain.User;
import org.csu.myProject.service.UserService;
import org.csu.myProject.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {

    private static final String LOGIN_FORM = "/WEB-INF/jsp/login.jsp";
    private static final String REGISTER_JSP = "/WEB-INF/jsp/register.jsp";

    private User user;
    private String confirmPassword;
    private String ageString;
    private String msg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        getParameters(req);
        if(validation()){
            UserService userService = new UserServiceImpl();
            int result = userService.reegister(this.user);
            if(result == 1){
                this.msg = "注册成功！请用您的用户名和密码进行登录";
                req.setAttribute("msg", this.msg);
                req.getRequestDispatcher(LOGIN_FORM).forward(req, resp);
            }else{
                if(userService.getMsg() == null || userService.getMsg().equals(""))
                {
                    req.setAttribute("msg", "服务器错误，请稍后重试");
                }else {
                    req.setAttribute("msg", userService.getMsg());
                }
                req.getRequestDispatcher(REGISTER_JSP).forward(req, resp);
            }
        }else{
            req.setAttribute("msg", this.msg);
            req.getRequestDispatcher(REGISTER_JSP).forward(req, resp);
        }
    }

    private boolean validation(){
        if(this.user.getUsername() == null || this.user.getUsername().trim().equals("")){
            this.msg = "用户名不能为空";
            return false;
        }
        if(this.user.getPassword() == null || this.user.getPassword().trim().equals("")){
            this.msg = "密码不能为空";
            return false;
        }
        if(this.confirmPassword == null || this.confirmPassword.trim().equals("")){
            this.msg = "重复密码不能为空";
            return false;
        }
        if(!this.user.getPassword().equals(this.confirmPassword)){
            this.msg = "两次密码不一致";
            return false;
        }
        try{
            int age = Integer.parseInt(this.ageString);
            this.user.setAge(age);
        }catch (Exception e){
            this.msg = "年龄不能为空且必须为整数";
            return false;
        }
        if(this.user.getGender() == null || this.user.getGender().trim().equals("")){
            this.msg = "性别不能为空";
            return false;
        }
        return true;
    }

    private void getParameters(HttpServletRequest request){
        this.user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        this.confirmPassword = request.getParameter("confirmPassword");
        user.setGender(request.getParameter("gender"));
        this.ageString = request.getParameter("age");
        user.setEmail(request.getParameter("email"));
    }
}
